package com.emergency.embackend.controller;

import com.emergency.embackend.common.PageParams;
import com.emergency.embackend.common.R;
import com.emergency.embackend.entity.Share;
import com.emergency.embackend.entity.ShareComment;
import com.emergency.embackend.exception.EmException;
import com.emergency.embackend.service.OperateService;
import com.emergency.embackend.service.ShareDataService;
import com.emergency.embackend.utils.UserHolder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/share")
public class ShareDataControllor {
    @Autowired
    private ShareDataService shareDataService;
    @Autowired
    private OperateService operateService;
    //添加分享信息
    @PostMapping("/add")
    public R add(@RequestBody Share share){
        if (share==null){
            return R.error("参数不允许为空！");
        }
        shareDataService.add(share);
        return R.ok();
    }
    //删除分享信息
    @GetMapping("/delete")
    public R delete(@RequestParam("id") Long id){
        shareDataService.delete(id);
        return R.ok();
    }
    //更新分享信息
    @PostMapping("/update")
    public R update(@RequestBody Share share){
        if (share==null){
            return R.error("参数不允许为空！");
        }
        shareDataService.update(share);
        return R.ok();
    }
    //获取用户自己分享信息
    @PostMapping("/getShareByUser")
    public R getShareByUser(@RequestBody PageParams pageParams) {
        Long userId=UserHolder.getUser().getId();
        return R.ok().put("data",shareDataService.getShareByUser(userId,pageParams));
    }
    @PostMapping("/pageAll")
    public R pageAll(@RequestBody PageParams pageParams){
        return R.ok().put("data",shareDataService.getAll(pageParams));
    }
    //获取某个分享
    @GetMapping("/getOne")
    public R getOne(@RequestParam("id") Long Id){
        if(Id==null){
            throw new EmException("401","参数为空");
        }
        return R.ok().put("data",shareDataService.getOne(Id));
    }
    //获取用户某个分享
    @GetMapping("/getUserOne")
    public R getUserOne(@RequestParam("id") Long Id){
        if(Id==null){
            throw new EmException("401","参数为空");
        }
        return R.ok().put("data",shareDataService.getUserOne(Id));
    }
    //根据时间获取列表
    @PostMapping("/pageTime")
    public R pageTime(@RequestBody PageParams pageParams){
        return R.ok().put("data",shareDataService.pageTime(pageParams));
    }
    //获取审核列表
    @PostMapping("/underReview")
    public R underReview(@RequestBody PageParams pageParams){
        return R.ok().put("data",shareDataService.underReview(pageParams));
    }
    //审核
    @PostMapping("/changeStatus")
    public R changeStatus(@RequestParam("status") char status,Long id){
        if(status=='0'){
            throw new EmException("401","参数不能为空");
        }
        shareDataService.changeStatus(status,id);
        return R.ok();
    }
    @GetMapping("/like")
    public R like(@RequestParam("shareId") Long shareId){
        Integer isLike = operateService.like(shareId,'2');
        return R.ok().put("data",isLike);
    }
   @PostMapping("/addComment")
    public R addComment(@RequestBody ShareComment shareComment){
        shareDataService.addComment(shareComment);
        return R.ok();
   }

}
